package com.yan.furns.service.impl;

import com.yan.furns.dao.MemberDAO;
import com.yan.furns.dao.impl.MemberDAOImpl;
import com.yan.furns.entity.Member;
import com.yan.furns.service.MemberService;
import com.yan.furns.utils.MD5Utils;

import static com.alibaba.druid.util.Utils.md5;

/**
 * @author Yzb
 * @version 1.0
 */
public class MemberServiceImpl implements MemberService {
	private MemberDAO memberDAO = new MemberDAOImpl();

	@Override
	public boolean registerMember(Member member) {
		return memberDAO.saveMember(member) == 1;
	}

	/**
	 * 判断username是否存在
	 * @param username 用户名
	 * @return 如果存在返回true, 否则返回false
	 */
	@Override
	public boolean existUsername(String username) {
		Member member = memberDAO.queryMemberByUsername(username);
		return member != null;
	}

	@Override
	public Member login(String username, String password) {

		/*
		if (!existUsername(username)) {
			return null;
		}

		// overTODO 这里MD5加密有大问题!!
		Member member = memberDAO.queryMemberByUsername(username);
		String md5Pwd = MD5Utils.MD5(password);
//		System.out.println("member_pwd= " + member.getPassword());
//		System.out.println("pwd= " + password);
//		System.out.println(md5Pwd + " == " + member.getPassword());
		if (md5Pwd.equals(member.getPassword())) {
			return member;
		}
		 */
		// 将密码判断转到数据库
		return memberDAO.queryMemberNyUsernameAndPassword(username, password);
	}
}
